package exp03;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.Scanner;

public class I0 {
    public static void main(String[] args) {
        int n = cin.nextInt();
        PriorityQueue<Long> Q = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            Q.offer(cin.nextLong());
        }
        long cost=0;
        while(Q.size()>=2){
            long x=Q.peek();
            Q.poll();
            long y=Q.peek();
            Q.poll();
            cost+=x+y;
            // x+y add
            Q.offer(x+y);
        }
        System.out.println(cost);
        cin.close();
        cout.close();
    }

    static Scanner cin = new Scanner(new BufferedInputStream(System.in));//���������Ż��汾
    static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));//��������Ż��汾
    static PrintWriter cerr = new PrintWriter(System.err, true);//��������ģʽ,һ����������󣬲���Ҫbuffered
}
